// import {tagsEditRow, editFromValidate} from '../tags_list.js'

require('../layer.js')
import {token} from '../common.js'
let reqDefault = {
	token: token,
	pages: {
		page: 1,
		page_number: 10
	}
}
let template = require('../plugins/art-template.js');

require('jqPaginator/dist/1.2.0/jqPaginator.min.js');

let optionsObj = {}
let postObj = {}
let actionUrl = {}
var tableId

function listInit(url, postParams, tplid, tableid) {
	console.log(url)
	let tbody = $(tableid).find('tbody')
	tableId = tableid
	reqDefault.params = postParams;
	postObj.url = url
	postObj.tplId = tplid
	postObj.tbody = tbody
	$.ajax({
		type: "POST",
		url: url,
		data: reqDefault,
		dataType: "json",
		success: function(res) {
			if(res.status === 1) {
				if(res.msg === '暂无数据') {
					$(tableid).hide();
					$("#datatable-paginate ul").hide();
					$(".null").removeClass('hide')
				}else {
					$(tableid).show();
					$(".null").addClass('hide')
					$("#datatable-paginate ul").show();
					optionsObj.count = parseInt(res.data._total.count)
					optionsObj.current = parseInt(res.data._total.page)
					var $html = template(tplid,res.data);
	                tbody.html($html);
	                Page.init()
				}
			}
		}
	})
}

/*分页*/
var Page = {
	init: function() {
		var self = this;
		$.jqPaginator('#datatable-paginate ul', {
			totalCounts: optionsObj.count,
			pageSize: reqDefault.pages.page_number,
	        visiblePages: 10,
	        currentPage: optionsObj.current,
	        prev: '<li class="prev"><a href="javascript:;">Previous</a></li>',
	        next: '<li class="next"><a href="javascript:;">Next</a></li>',
	        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
	        onPageChange: function (num, type) {
	        	if(type === 'init') {
	        		console.log(type)
	        	}else {
	        		self.getPageData(num);
	        	}	
	        }
	    });
	},
	getPageData: function(num) {
		reqDefault.pages.page = num;
		$.ajax({
			type: 'POST',
			url: postObj.url,
			data: reqDefault,
			dataType: "json",
			success: function(res) {
				if(res.status === 1) {					
					optionsObj.count = parseInt(res.data._total.count);
					optionsObj.current = parseInt(res.data._total.page);
					var $html = template(postObj.tplId,res.data);
	                postObj.tbody.html($html);
				}
			}
		})
	}, 

};

/*设置获取单个详情修改的url*/
function setEditorUrl(url) {
	actionUrl.editUrl = url
}
/*设置修改完成后提交的url*/
function setEditorUpdateUrl(url) {
	actionUrl.editUpdateUrl = url
}

/*设置删除的url*/
function setDelUrl(url) {
	actionUrl.trDelUrl = url
}

var EditableTable
(function( $ ) {

	EditableTable = {

		options: {
			table: '',
			modal: '#myModal',
			dialog: {
				wrapper: '#dialog',
				cancelButton: '#dialogCancel',
				confirmButton: '#dialogConfirm',
			}
		},

		initialize: function() {
			this
				.setVars()
				.build()
				.events();
		},

		setVars: function() {
			this.options.table = tableId;
			this.$modal = $( this.options.modal );
			this.$table				= $( this.options.table );

			// dialog
			this.dialog				= {};
			this.dialog.$wrapper	= $( this.options.dialog.wrapper );
			this.dialog.$cancel		= $( this.options.dialog.cancelButton );
			this.dialog.$confirm	= $( this.options.dialog.confirmButton );

			return this;
		},

		build: function() {
			console.log(tableId)
			this.datatable = this.$table

			window.EditableTable = EditableTable;

			return this;
		},

		events: function() {
			var _self = this;

			this.$table
				.on( 'click', 'button.remove-row', function( e ) { //删除
					e.preventDefault();
					var $row = $(this).closest( 'tr' );
					var postdata = {};
					postdata.id = $(this).attr('data-id');
					postdata.token = token
					$.magnificPopup.open({
						items: {
							src: _self.options.dialog.wrapper,
							type: 'inline'
						},
						preloader: false,
						modal: true,
						callbacks: {
							change: function() {
								_self.dialog.$confirm.on( 'click', function( e ) {
									e.preventDefault();

									_self.rowRemove( $row, postdata);
									$.magnificPopup.close();
								});
							},
							close: function() {
								_self.dialog.$confirm.off( 'click' );
							}
						}
					});
				});

			
			this.dialog.$cancel.on( 'click', function( e ) {
				e.preventDefault();
				$.magnificPopup.close();
			});

			// this.$modal.find('.save-change').on('click', function(e) {
			// 	e.preventDefault()
			// 	let postData = editFromValidate(_self.options.modal)
			// 	if(!$.isEmptyObject(postData)) {
			// 		_self.submit(postData, _self.$row)
			// 	}
			// })
			return this;
		},
		

		
		// rowEdit: function( post, modalid, $row) {
		// 	var _self = this;
		// 	this.$row = $row
		// 	if(/tagtype/g.test(actionUrl.editUrl)) { //表示标签分类修改
		// 		tagsEditRow(post, modalid)	
		// 	}
		// },
		// submit: function(post, $row) {
		// 	var _self = this;
		// 	$.ajax({
		// 		type: 'POST',
		// 		url: actionUrl.editUpdateUrl,
		// 		data: post,
		// 		dataType: "json",
		// 		success: function(res) {
		// 			if(res.status === 1) {
		// 				_self.$modal.modal('hide')
		// 				$.ajax({
		// 					type: "POST",
		// 					url: actionUrl.editUrl,
		// 					data: {
		// 						id: post.id
		// 					},
		// 					dataType: "json",
		// 					success: function(res) {
		// 						if(res.status === 1) {
		// 							layer.msg('数据修改成功', {time:500});
		// 							var $trhtml = template('update',res.data);
	 //                				$row.html($trhtml);

		// 						}
		// 					}
		// 				})
		// 			}else {
		// 				_self.$modal.modal('show')
		// 			}
		// 		}
		// 	})
		// },

		rowRemove: function( $row, postdata) { //单个的删除
			var _self = this;
			if ( $row.hasClass('adding') ) {
				this.$addButton.removeAttr( 'disabled' );
			}
			
			/*发送删除的ajax请求*/
			$.ajax({
				type: 'POST',
				url: actionUrl.trDelUrl,
				data: postdata,
				dataType: "json",
				success: function(res) {
					if(res.status === 1) {
						_self.datatable.find($row).remove();
						layer.msg('删除成功', {time:500});
						
					}else {
						layer.msg('删除成功失败，稍后重试', {time:500});
						
					}
				}

			})	
		},

		

	};

	$(function() {
		// EditableTable.initialize();
	});


}).apply( this, [$])


export{listInit, reqDefault, setEditorUrl, setEditorUpdateUrl, setDelUrl, EditableTable}

